<?php
header('Content-Type: application/json');

try {
    // 读取 POST 数据
    $data = json_decode(file_get_contents('php://input'), true);
    
    if (!isset($data['name']) || !isset($data['phone']) || !isset($data['content'])) {
        throw new Exception('数据格式错误');
    }

    // 读取现有意见
    $feedbackFile = '../admin/data/feedback.json';
    if (!file_exists($feedbackFile)) {
        file_put_contents($feedbackFile, json_encode(['feedback' => [], 'next_id' => 1]));
    }
    
    $feedback = json_decode(file_get_contents($feedbackFile), true);
    if (!isset($feedback['next_id'])) {
        $feedback['next_id'] = 1;
    }
    
    // 添加新意见
    $feedback['feedback'][] = [
        'id' => $feedback['next_id']++,
        'name' => $data['name'],
        'phone' => $data['phone'],
        'content' => $data['content'],
        'created_at' => $data['created_at'],
        'status' => 'pending'
    ];
    
    if (file_put_contents($feedbackFile, json_encode($feedback, JSON_PRETTY_PRINT))) {
        echo json_encode(['success' => true]);
    } else {
        throw new Exception('保存失败');
    }

} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?> 